import store from "@/store"

// 此vm参数为页面的实例，可以通过它引用vuex中的变量
module.exports = vm => {
  // 初始化请求配置
  uni.$u.http.setConfig(config => {
    /* config 为默认全局配置*/
    if (process.env.NODE_ENV === "development") {
      // 开发环境
      config.baseURL = "/api/target"
    } else {
      // 生产
      config.baseURL = "/api/target"
    }

    setBaseUrl(config)

    console.log(config, "config");
    return config
  })

  // 请求拦截
  uni.$u.http.interceptors.request.use(

    config => {
      const { modalLoading } = store.getters

      setBasicParams(config)
      console.log(config, "configconfig");
      // 接口为对话框时不需要弹起全局加载框
      if (!modalLoading) {
        store.commit("app/SET_GLOBAL_LOADING", true);
      }
      // uni.showLoading()

      return config
    },
    config => {
      // 可使用async await 做异步操作
      // uni.hideLoading()
      return Promise.reject(config)
    }
  )

  // 响应拦截
  uni.$u.http.interceptors.response.use(
    response => {
      // uni.hideLoading()
      closeLoading()
      /* 对响应成功做点什么 可使用async await 做异步操作*/
      const { code, data, message } = response.data || {}
      if (response.config.url === "/mes/receive/excelItem") {
        return response.data
      }
      switch (code) {
        case 2000:
          return data
        case 4003:
          store.commit("app/SET_MESSAGE", {
            message: "登录过期即将返回登录页面", status: 2
          });
          setTimeout(() => {
            uni.$u.route({
              url: "pages/login/login",
              type: "redirectTo"
            })
          }, 1000)
          return Promise.reject(response)
        default:
          store.commit("app/SET_MESSAGE", { message, status: 2 });
          return Promise.reject(response)
      }
    },
    response => {
      const { statusCode } = response
      let message = ""
      switch (statusCode) {
        case 502:
          message = "网络异常请稍后尝试"
          break;

        default:
          message = "error"
          break;
      }
      store.commit("app/SET_MESSAGE", { message, status: 2 });
      closeLoading()
      return Promise.reject(response)
    }
  )
}

const closeLoading = () => {
  setTimeout(() => {
    store.commit("app/SET_GLOBAL_LOADING", false);
    store.commit("app/SET_MODAL_LOADING", false);
  }, 200);
}

const setBasicParams = (config) => {
  config.data = config.data || {}
  const { rsk_token, user_obj } = store.state
  const { username } = user_obj || {}
  if (rsk_token) {
    config.header["TOKEN"] = rsk_token
  }
  const { method } = config
  if (method === "GET") {
    config.params = {
      ...config.params,
      douser: username,
      systemCode: "MES",
      userAccount: "dian",
      userCode: "dian"

    }
  } else if (method === "POST") {
    config.data = {
      ...config.data,
      douser: username,
      systemCode: "MES",
      userAccount: "dian",
      userCode: "dian"
    }
  }
}

const setBaseUrl = config => {
  // #ifdef APP-PLUS
  if (process.env.NODE_ENV === "development") {
    // 开发环境
    config.baseURL = "http://portal.cnrecyc.com/api/target"
  }
  // #endif
}
